﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TraceThisItem.Domain;

namespace TraceThisItem.WebUI.Items
{
	public partial class Create : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}


		protected void ItemTypesDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["projectId"] = ProjectDropDownList.SelectedValue;
		}

		protected void ItemTypesDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void ProjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void ProjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["userName"] = User.Identity.Name;
		}

		protected void ProjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
		{
			IEnumerable<Project> projects = (IEnumerable<Project>)(e.ReturnValue);
			if (projects.Any() == false)
			{
				lblBigMessage.Text = "No existen proyectos a donde agregar nuevas incidencias";
				btnAgregarIncidencia.Enabled = false;
				lblBigMessage.Visible = true;
			}
		}

		protected void ItemTypesDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
		{
			IEnumerable<ItemType> selectedItemTypes = (IEnumerable<ItemType>)e.ReturnValue;
			if (selectedItemTypes.Any() == false)
			{
				lblBigMessage.Text = "No existen tipos de item a donde agregar nuevas incidencias";
				btnAgregarIncidencia.Enabled = false;
				lblBigMessage.Visible = true;
			}
			else
			{
				btnAgregarIncidencia.Enabled = true;
				lblBigMessage.Visible = false;
			}
		}

		protected void btnAgregarIncidencia_Click(object sender, EventArgs e)
		{
			if (Page.IsValid)
			{
				Item newItem = new Item();
				newItem.CreatedOn = System.DateTime.Now;
				newItem.Comments = txtComentarios.Text;
				newItem.Description = txtDescripcion.Text;
				newItem.LatestUpdate = newItem.CreatedOn;
				newItem.Priority = Convert.ToByte(PriorityDropDownList.SelectedValue);
				newItem.Project = Facade.Instance.GetProjectById(Convert.ToInt32(ProjectDropDownList.SelectedValue));
				newItem.Subject = txtAsunto.Text;
				newItem.Team = Facade.Instance.GetTeamById(Convert.ToInt32(TeamsDropDownList.SelectedValue));
				newItem.Type = Facade.Instance.GetItemTypeById(Convert.ToInt32(ItemTypesDropDownList.SelectedValue));
				newItem.Traceability = new List<ItemStatus>();
				newItem.Traceability.Add(new ItemStatus
										{
											Date = System.DateTime.Now,
											Responsible = Facade.Instance.GetUser(ResponsableDropDownList.SelectedValue),
											StatusNode = newItem.Type.Workflow.Nodes.ToList()[0]

										});
				Facade.Instance.AddItem(newItem);
				Response.Redirect("~/Default.aspx",false);
			}
		}

		protected void ResponsableDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void ResponsableDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["projectId"] = ProjectDropDownList.SelectedValue;
		}

		protected void ResponsableDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
		{
			IEnumerable<User> selectedItemTypes = (IEnumerable<User>)e.ReturnValue;
			if (selectedItemTypes.Any() == false)
			{
				lblBigMessage.Text = "No hay responsables disponibles";
				btnAgregarIncidencia.Enabled = false;
				lblBigMessage.Visible = true;
			}
		}

		protected void TeamsDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

	}
}